package net.bblot.lottery.api.request;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.google.gson.Gson;
import net.bblot.lottery.PackegeConfig;
import net.bblot.lottery.api.ApiResponseListener;
import net.bblot.lottery.api.task.HttpTask;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;

public class GetIp {
	public static HttpTask getAuth(Context context, String mac, ApiResponseListener listener) {
		String url = PackegeConfig.Wifi_base_url + "/grant/getAuthInfoByMac";
		// String url = "http://www.baidu.com";
		List<NameValuePair> queryParams = new ArrayList<NameValuePair>();
		queryParams.add(new BasicNameValuePair("app_mac", mac));
		final Handler handler = HttpTask.getHttpHandler(listener);
		return HttpTask.getTask(handler, context, url, HttpTask.HTTP_REQEST_TYPE_GET, queryParams, null, null, null);
	}

	public static String parseFromAuthIfo(String str) {
		if (TextUtils.isEmpty(str)) {
			return null;
		}
		String result = null;
		int i = 0;
		boolean isfind = false;
		while (i >= 0 && i < str.length()) {
			// 从i+1处向后找var的位置，
			i = str.indexOf("var", i + 1);
			if (i > 0) {
				String tmp = str.substring(i, i + 20);
				if (tmp.contains("gw_parameter")) {
					isfind = true;
					break;
				}
			}
		}
		if (!isfind) {
			return null;
		} else {
			int n = str.indexOf("{", i);
			int m = str.indexOf("}", i);
			if (n > 0 && m > 0) {
				result = str.substring(n, m + 1);
			}
			return result;
		}

	}

	public static boolean isAuth(String result) {
		boolean isAuth = false;
		try {
			Gson gson = new Gson();
			Result result2 = gson.fromJson(result, Result.class);
			if (result2.auth_stat == 1) {
				isAuth = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return isAuth;

	}

	public static class Result {
		public static final int AUTN_STAT_ALREADY = 1;
		public String code;
		public int auth_stat;
		public IpParm ap_info;

		public boolean isOk() {
			return ap_info != null;
		}
	}

	public static class IpParm {
		public String ap_series;

		public String terminal_ip;

		public String terminal_mac;

		/**
		 * 将json串拼接成qurrey参数
		 * 
		 * @param str
		 * @return
		 * @throws Exception
		 */
		public static String toWifiQurrayParm(IpParm str) throws Exception {
			StringBuilder sb = new StringBuilder("?pageName=wifiGrant");
			sb.append("&" + "gw_id" + "=" + str.ap_series);
			sb.append("&" + "ip" + "=" + str.terminal_ip);
			sb.append("&" + "mac" + "=" + str.terminal_mac);
			return sb.toString();
		}

		/**
		 * 将json串拼接成qurrey参数
		 * 
		 * @param str
		 * @return
		 * @throws Exception
		 */
		public static String toWifiQurrayParm(String str) throws Exception {
			StringBuilder sb = new StringBuilder("?pageName=wifiGrant");
			JSONObject jObject = new JSONObject(str);
			Iterator<String> iterator = jObject.keys();
			while (iterator.hasNext()) {
				String key = iterator.next();
				sb.append("&" + key + "=" + jObject.getString(key));
			}
			return sb.toString();
		}
	}
}
